package face.pay.api.cpn.service.impl;

import com.alibaba.fastjson.JSON;
import face.pay.api.cpn.param.QueryCpnParam;
import face.pay.api.cpn.service.ICpnCardService;
import face.pay.dao.CpnCardDetailInfoDao;
import face.pay.dao.TerminalInfoDao;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.TerminalInfo;
import face.pay.util.ResultMessage;
import face.pay.vo.VCpnCardDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CpnCardServiceImpl implements ICpnCardService {

    @Autowired
    TerminalInfoDao terminalInfoDao;

    @Autowired
    private CpnCardDetailInfoDao cpnCardDetailInfoDao;

    @Override
    public ResultMessage queryData(String json) {

        log.info("优惠券查询："+json);

        QueryCpnParam cpnParam = JSON.parseObject(json,QueryCpnParam.class);

        String deviceId = cpnParam.getDevice_id();

        Map<String,Object>  data = new HashMap<>();
        Integer cardNums = 0;
        List<VCpnCardDetail> list =  new ArrayList<>();

        String openId = cpnParam.getOpenid();

        if(StringUtils.isNotBlank(deviceId) ) {

            deviceId = deviceId.toUpperCase();

            TerminalInfo terminalInfo = this.terminalInfoDao.getByDeviceId(deviceId);

            VCpnCardDetail cardDetail = new VCpnCardDetail();

            cardDetail.setOemId(terminalInfo.getOemId());

            cardDetail.setOpenid(openId);

            cardDetail.setMchUserId(terminalInfo.getUserId());
            cardDetail.setStoreId(terminalInfo.getStoreId());
            list =  cpnCardDetailInfoDao.queryUsableCards(cardDetail);
            cardNums  = list.size();
        }
        data.put("cardNums",cardNums);
        data.put("cardList",list);

        return ResultMessage.createSuccess(data);
    }
}
